package booking.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtils {
	public int timePiece = 0;
	public int timeScale = 0;

	/**
	 * 
	 * @param 将一个小时分成多少分1到60
	 */
	public TimeUtils(int timeScale) {
		if (timeScale > 60 || timeScale < 1) {
			timeScale = 1;
		}
		int timePiece = 24 * timeScale;
		this.timePiece = timePiece;
		this.timeScale = timeScale;
	}

	public String timeIndexToString(int index) {
		int hour = index / timeScale;
		int minute = (index % timeScale) * (60 / timeScale);
		String timeFmt = hour + ":" + ((minute == 0) ? "00" : minute);
		return timeFmt;
	}

	public int timeIndex(Date date) {
		DateFormat hourFormat = new SimpleDateFormat("HH");
		String hourTime = hourFormat.format(date);
		DateFormat minFormat = new SimpleDateFormat("mm");
		String minTime = minFormat.format(date);
		int min = Integer.valueOf(minTime).intValue();
		int minIndex = 0;
		if (min > 30) {
			minIndex += 2;
		} else if (min > 0) {
			minIndex += 1;
		}
		int index = Integer.valueOf(hourTime).intValue() * timeScale + minIndex;
		return index;
	}

	public int timeIndexNow() {
		// Date d = new Date();
		// int hours = d.getHours();
		// return hours * timeScale;
		Date date = new Date();
		return timeIndex(date);
	}

	public void spend(int index) {

	}
}
